using Avalonia.Controls;
using System;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Net.Http;
using System.Threading.Tasks;
using Avalonia.Controls.Notifications;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using 第五次作业.utils;

namespace 第五次作业;

public partial class MainWindow : Window
{
    private SearchScript _searchScript = new SearchScript();
    private WindowNotificationManager? _notificationManage;
    public MainWindow()
    { 
        InitializeComponent();
        
    }

    private async void SearchButtonClicked(object sender, RoutedEventArgs e)
    {
        string? keyword = TextInput.Text;
        if (keyword == null)
        {
            _notificationManage = new WindowNotificationManager(this)
            {
                Position = NotificationPosition.TopLeft,
                MaxItems = 3
            };
            _notificationManage.Show(new Notification("错误", "关键词不能是空", NotificationType.Error));
        }

        List<string> urlList;
        
        urlList = await _searchScript.Search(keyword, 100);
        List<(string Url, string PhoneNumber)> phoneNumbers = await _searchScript.ScrapePhoneNumbers(urlList, 100);

        foreach (var phoneNumber in phoneNumbers)
        {
            UrlList.Items.Add($"电话号码：{phoneNumber.PhoneNumber}\tURL：{phoneNumber.Url}");
        }
    }
}